package org.sjsu.freedoor.model;

import org.json.JSONException;
import org.json.JSONObject;
import org.sjsu.freedoor.util.Utilities;

import com.mongodb.DBObject;

public class Service {
	private String m_url;
	private String m_id;
	private String m_name;
	private String m_type;
	private String m_description;
	private String m_status;
	private String m_timestamp;
	private OfferList m_offers;
	
	public Service() {

	}
	
	public Service(String url, String id, String name, String type, String description, String status, String timestamp, OfferList offers) {
		m_url = url;
		m_id = id;
		m_name = name;
		m_type = type;
		m_description = description;
		m_status = status;
		m_timestamp = timestamp;
		m_offers = offers;
	}
		
	public boolean loadService(JSONObject jsonObj) {
		try {
			Freedoor.getInstance(); //make sure FreeDoor and SELF_URL is initialized;
			
			m_url = jsonObj.optString("community_url");
			m_id = jsonObj.optString("id");
			m_name = jsonObj.getString("name");
			m_type = jsonObj.getString("type");
			m_description = jsonObj.getString("description");
			m_status = jsonObj.getString("status");
			m_timestamp = jsonObj.getString("timestamp");
			
			if(m_url == "")
				m_url = Freedoor.SELF_URL;
			if(m_url == Freedoor.SELF_URL)
				m_id = ""; //set to empty, Mongo will set to auto-generated
		} catch (JSONException e) {
			System.out.println("Invalid Service jsonObj: " + jsonObj.toString());
			return false;
		}
		
		try {
			m_offers = new OfferList();
			m_offers.loadOffers(jsonObj.getJSONArray(OfferList.OFFERS_ARRAY_KEY)); //not requiring offers right now -- return this response if want to require
		} catch (JSONException e) {
			System.out.println("No Offers in jsonObj: " + jsonObj.toString());
		}
		return true;
	}
	
	public boolean loadService(DBObject dbObj) {
		try {
			m_url = dbObj.get("community_url").toString();
			m_id = dbObj.get("id").toString();
			m_name = dbObj.get("name").toString();
			m_type = dbObj.get("type").toString();
			m_description = dbObj.get("description").toString();
			m_status = dbObj.get("status").toString();
			m_timestamp = dbObj.get("timestamp").toString();

			m_offers = new OfferList();
			m_offers.loadOffers(dbObj.get(OfferList.OFFERS_ARRAY_KEY).toString()); //not requiring offers right now -- return this response if want to require
		} catch (NullPointerException e) {
			System.out.println("Invalid DBObject: " + dbObj.toString());
		}
		return !(m_url.isEmpty() || m_id.isEmpty()/* || m_name.isEmpty() ||
				m_type.isEmpty() || m_description.isEmpty() || m_status.isEmpty() ||
				m_timestamp.isEmpty()*/); //not being too strict here for now
	}
	
	public boolean loadService(String jsonString) {
		try {
			JSONObject obj = new JSONObject(jsonString);
			
			return loadService(obj);
		} catch (JSONException e) {
			System.out.println("Invalid Service jsonString: " + jsonString);
			return false;
		}
	}
	
	public void setURL(String url) {
		m_url = url;
	}
	
	public String getURL() {
		return m_url;
	}
	
	public void setId(String id) {
		m_id = id;
	}
	
	public String getId() {
		return m_id;
	}
	
	public void setName(String name) {
		m_name = name;
	}
	
	public String getName() {
		return m_name;
	}
	
	public void setType(String type) {
		m_type = type;
	}
	
	public String getType() {
		return m_type;
	}
	
	public void setDescription(String description) {
		m_description = description;
	}
	
	public String getDescription() {
		return m_description;
	}
	
	public void setStatus(String status) {
		m_status = status;
	}
	
	public String getStatus() {
		return m_status;
	}
	
	public void setTimeStamp(String timestamp) {
		m_timestamp = timestamp;
	}
	
	public String getTimeStamp() {
		return m_timestamp;
	}
	
	public void setOffers(OfferList offers) {
		m_offers = offers;
	}
	
	public OfferList getOffers() {
		return m_offers;
	}
	
	public boolean addOffer(Offer ofr) {
		return m_offers.add(ofr);
	}
	
	public boolean addOffers(OfferList ofrs) {
		boolean rslt = false;
		for(Offer o : ofrs.getList())
			rslt = rslt || m_offers.add(o);
		return rslt;
	}
	
	public String toJsonString() {
		String str  = "{\"community_url\":\"" + m_url + "\", \"id\":\"" + m_id + "\", \"name\":\"" + m_name + "\", \"type\":\"" + m_type + "\", \"description\":\"" + m_description + "\", \"status\":\"" + m_status + "\", \"timestamp\":\"" + m_timestamp + "\", ";
		str += m_offers.toJsonString(false, true).replaceAll("\\\"", "\""); //get rid of escaped character sequences
		str += "}";
		return str;
	}
	
	public JSONObject toJsonObject()
	{
		return Utilities.toJsonObject(this.toJsonString());
	}

	public String getFullRestfulUri() {
		return getURL() + "/services/" + getId();
	}
}
